<?php
namespace app\mobile\controller;

use app\model\Theme; 
use app\model\Poster; 
use app\model\Worker; 

class WExample extends Base
{
    public function W_example()
    {
global $_G, $_P;
// $fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('管理后台地址不正确');
}
$system=$_G['system'];
$theme = Theme::getThemeById($themeid);
$config = 	Theme::getConfig($themeid);
if(empty($theme['id'])){
	return_msg('管理后台地址不正确');
}
$myworker = Worker::getThemeWorkerByOpenidNew($_G['openid'],$themeid);
if(!($system['ruanjian']!=3 || $config['showshop']==0 || ($system['ruanjian']==3 && $config['showshop']==$myworker['company']))){
	$myworker = "";
}
if (empty($myworker)) {
	return_msg('您不是员工,无权访问');
}
$msg = Worker::gethoutaistats();
if (!empty($msg)) {
	return_msg($msg);
}
$do = 'example';
$id = intval($_P['id']);
if ($id>0){
	$sql = "SELECT r.addtime,r.status,r.isrefund,r.nickname,r.avatar,o.uniontid,o.transaction_id,o.realfee FROM " . tablename ( t_record ) . " as r
		left join " . tablename ( t_order ) . " as o on r.id = o.masterid
		WHERE r.themeid ='{$themeid}' and o.paystate=1 and o.realfee>0 and r.id ='{$id}'";
		//echo $sql;
	$order  = db_find ( $sql );

	if (empty($order)){
		return_msg("错误:未付款/已退款/线下定单均无凭证");
	}elseif ($order['status'] == 0){
		return_msg("此定单未付款");
	}elseif ($order['status'] < 0){
		if ($item['isrefund']==2){
			return_msg("此定单已自动退款");
		}else{
			return_msg("此定单已手工退款");
		}
	}
}else{
	//return_msg("自动生成定单");
	$kaishishu = rand(1,700);
	$xuni1 = db_find ("SELECT nickname,username,avatar FROM " . tablename('2019xuni_fans') . " where id =". $kaishishu );

	$kaishishu = rand(1,700);
	$xuni2 = db_find ("SELECT nickname as username,avatar FROM " . tablename('2019xuni_fans') . " where	id =". $kaishishu );

	//2个价格
	if ($system['jiage']==0) {
		if ($config['joinfee'] != "0.00"){
			$realfee=$config['joinfee'];
		}
	}elseif ($system['jiage']==1) {
		if ($config['joinfee'] != "0.00" && $config['joinfee2'] != "0.00"){
			if (rand(0, 10)<6){
				$realfee=$config['joinfee'];
			}else{
				$realfee=$config['joinfee2'];
			}
		}else{
			if ($config['joinfee'] != "0.00"){
				$realfee=$config['joinfee'];
			}else{
				$realfee=$config['joinfee2'];
			}
		}
	}elseif ($system['jiage']==2) {
		if ($config['joinfee'] != "0.00" && $config['joinfee2'] != "0.00" && $config['joinfee3'] != "0.00"){
			if (rand(0, 10)<4){
				$realfee=$config['joinfee'];
			}elseif (rand(0, 10)<6){
				$realfee=$config['joinfee2'];
			}else{
				$realfee=$config['joinfee3'];
			}
		}
	}
	//2个拼团价
	if (empty($realfee) || $realfee == "0.00"){
		if ($system['ruanjian']==4) {
			if ($config['groupprice2'] != "0.00" && $config['groupprice'] != "0.00" && $config['primeprice'] != "0.00"){
				if (rand(0, 10)<4){
					$realfee=$config['groupprice2'];
				}elseif (rand(0, 10)<6){
					$realfee=$config['groupprice'];
				}else{
					$realfee=$config['primeprice'];
				}
			}else{
				if ($config['groupprice'] != "0.00"){
					$realfee=$config['groupprice'];
				}else{
					$realfee=$config['primeprice'];
				}
			}
		}
	}
	if (empty($realfee) || $realfee == "0.00"){
		if ($config['groupprice'] != "0.00" && $config['primeprice'] != "0.00"){
			if (rand(0, 10)<7){
				$realfee=$config['groupprice'];
			}else{
				$realfee=$config['primeprice'];
			}
		}else{
			if ($config['groupprice'] != "0.00"){
				$realfee=$config['groupprice'];
			}else{
				$realfee=$config['primeprice'];
			}
		}
	}

	$transaction_id = "42000002" . rand(51,96) . date("YmdHis"). rand(10000,99999)  . rand(10000,99999);
	$uniontid = getMillisecond();
	$uniontid = date("Y").mb_substr($uniontid,4,24,'utf-8');
	$order = array(
		'addtime' => TIMESTAMP, 
		'nickname' =>$xuni1['nickname'], 
		'avatar' =>$xuni2['avatar'], 
		'realfee' =>$realfee, 
		'transaction_id' => $transaction_id, 
		'uniontid' => $uniontid, 
	);

}
	$sharedata = array(
	    'themeid'=>$themeid,
	    'fopenid'=>$_G['openid'],
	    'fromuid'=>$_G['member']['uid'],
	    'workerid'=>$myworker['uid'],
	    'flag'=>1,
	);
	
	$xiaoshu = round(($order['realfee']-floor($order['realfee']))*100);

    if ($system['gongzhong'] =='1'){
    	$domaintxt = $system['domainhead1'];
    }else{
    	$domaintxt = DOMAIN0;
    }
	$workerurl = "https://".$domaintxt."/".createMobileUrl('index',$sharedata);
	
	$workerurl = Poster::GetExampleUrl($system,$workerurl,MODULE_NAME ."员工:".$myworker['username']);

$return_clicent = get_defined_vars();
return view('/yxworker/w_example', $return_clicent);

// if($_G['companyname'] =='飞驰软件'){
// 	$return_clicent = get_defined_vars();
return view('/fcworker/fc_example' , $return_clicent);
// }else{
// 	$return_clicent = get_defined_vars();
return view('/yxworker/w_example' , $return_clicent);
// }
}
}